﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Linq;
using System.Text;

using Eforcity.Common35;
using Eforcity.Data.Common35;
using Eforcity.EbayApiDb;
using Eforcity.EbayApi.Ebay.TradingApi.Webservice;

namespace Eforcity.EbayApi
{
    public partial class EbayTradingApi : IDisposable
    {
        private Dictionary<string, byte> _CurencyCodeTypeDict;
        private Dictionary<string, byte> _SiteCodeTypeDict;
        private Dictionary<string, byte> _ListingTypeCodeTypeDict;
        private Dictionary<string, byte> _ListingDurationCodeTypeDict;
        private Dictionary<string, byte> _ShippingServiceCodeTypeDict;
        private Dictionary<string, byte> _BidActionCodeTypeDict;
        private Dictionary<string, byte> _GalleryCodeTypeDict;
        private Dictionary<string, byte> _CommentTypeCodeTypeDict;
        private Dictionary<string, byte> _MessageStatusTypeCodeTypeDict;
        private Dictionary<string, byte> _MessageTypeCodeTypeDict;
        private Dictionary<string, byte> _QuestionTypeCodeTypeDict;
        private Dictionary<string, byte> _ListingEnhancementCodeTypeDict;
        private Dictionary<string, byte> _DisputeReasonCodeTypeDict;
        private Dictionary<string, byte> _DisputeExplanationCodeTypeDict;
        private Dictionary<string, byte> _DisputeCreditEligibilityCodeTypeDict;
        private Dictionary<string, byte> _DisputeRecordTypeCodeTypeDict;
        private Dictionary<string, byte> _DisputeStateCodeTypeDict;
        private Dictionary<string, byte> _DisputeStatusCodeTypeDict;
        private Dictionary<string, byte> _DisputeMessageSourceCodeTypeDict;

        #region Transaction Related
        private Dictionary<string, byte> _CompleteStatusCodeTypeDict;
        private Dictionary<string, byte> _CheckOutStatusCodeTypeDict;
        private Dictionary<string, byte> _PaymentStatucCodeTypeDict;
        #endregion
        
        private void PreLoadCodeType()
        {
            
            //PreloadCodeTypes Table to Dictionary
            _CurencyCodeTypeDict = EbayCodeTypeData.GetCurrencyCodeType(_EbayStatConn);
            _SiteCodeTypeDict = EbayCodeTypeData.GetSiteCodeType(_EbayStatConn);
            _ListingTypeCodeTypeDict = EbayCodeTypeData.GetListingTypeCodeType(_EbayStatConn);
            _ListingDurationCodeTypeDict = EbayCodeTypeData.GetListingDurationCodeType(_EbayStatConn);
            _BidActionCodeTypeDict = EbayCodeTypeData.GetBidActionCodeType(_EbayStatConn);
            _ShippingServiceCodeTypeDict = EbayCodeTypeData.GetShippingServiceCodeType(_EbayStatConn);
            _GalleryCodeTypeDict = EbayCodeTypeData.GetGalleryTypeCodeType(_EbayStatConn);
            _CommentTypeCodeTypeDict = EbayCodeTypeData.GetCommentTypeCodeType(_EbayStatConn);
            _MessageStatusTypeCodeTypeDict = EbayCodeTypeData.GetMessageStatusTypeCodeType(_EbayStatConn);
            _MessageTypeCodeTypeDict = EbayCodeTypeData.GetMessageTypeCodeType(_EbayStatConn);
            _QuestionTypeCodeTypeDict = EbayCodeTypeData.GetQuestionTypeCodeType(_EbayStatConn);
            _ListingEnhancementCodeTypeDict = EbayCodeTypeData.GetListingEnhancementCodeType(_EbayStatConn);
            _CompleteStatusCodeTypeDict = EbayCodeTypeData.GetCompleteStatusCodeTypeDict(_EbayStatConn);
            _CheckOutStatusCodeTypeDict = EbayCodeTypeData.GetCheckOutStatusCodeTypeDict(_EbayStatConn);
            _PaymentStatucCodeTypeDict = EbayCodeTypeData.GetPaymentStatusCodeTypeDict(_EbayStatConn);
        }
        private void PreLoadCodeType(bool preloadDisputeRelatedCodeType)
        {
            //PreloadCodeTypes Table to Dictionary
            _CurencyCodeTypeDict = EbayCodeTypeData.GetCurrencyCodeType(_EbayStatConn);
            _SiteCodeTypeDict = EbayCodeTypeData.GetSiteCodeType(_EbayStatConn);
            _ListingTypeCodeTypeDict = EbayCodeTypeData.GetListingTypeCodeType(_EbayStatConn);
            _ListingDurationCodeTypeDict = EbayCodeTypeData.GetListingDurationCodeType(_EbayStatConn);
            _BidActionCodeTypeDict = EbayCodeTypeData.GetBidActionCodeType(_EbayStatConn);
            _ShippingServiceCodeTypeDict = EbayCodeTypeData.GetShippingServiceCodeType(_EbayStatConn);
            _GalleryCodeTypeDict = EbayCodeTypeData.GetGalleryTypeCodeType(_EbayStatConn);
            _CommentTypeCodeTypeDict = EbayCodeTypeData.GetCommentTypeCodeType(_EbayStatConn);
            _MessageStatusTypeCodeTypeDict = EbayCodeTypeData.GetMessageStatusTypeCodeType(_EbayStatConn);
            _MessageTypeCodeTypeDict = EbayCodeTypeData.GetMessageTypeCodeType(_EbayStatConn);
            _QuestionTypeCodeTypeDict = EbayCodeTypeData.GetQuestionTypeCodeType(_EbayStatConn);
            _ListingEnhancementCodeTypeDict = EbayCodeTypeData.GetListingEnhancementCodeType(_EbayStatConn);
            _CompleteStatusCodeTypeDict = EbayCodeTypeData.GetCompleteStatusCodeTypeDict(_EbayStatConn);
            _CheckOutStatusCodeTypeDict = EbayCodeTypeData.GetCheckOutStatusCodeTypeDict(_EbayStatConn);
            _PaymentStatucCodeTypeDict = EbayCodeTypeData.GetPaymentStatusCodeTypeDict(_EbayStatConn);
            if (preloadDisputeRelatedCodeType)
            {
                _DisputeReasonCodeTypeDict = EbayCodeTypeData.GetDisputeReasonCodeType(_EbayStatConn);
                _DisputeExplanationCodeTypeDict = EbayCodeTypeData.GetDisputeExplanationCodeType(_EbayStatConn);
                _DisputeCreditEligibilityCodeTypeDict = EbayCodeTypeData.GetDisputeCreditEligibilityCodeType(_EbayStatConn);
                _DisputeRecordTypeCodeTypeDict = EbayCodeTypeData.GetDisputeRecordTypeCodeType(_EbayStatConn);
                _DisputeStateCodeTypeDict = EbayCodeTypeData.GetDisputeStateCodeType(_EbayStatConn);
                _DisputeStatusCodeTypeDict = EbayCodeTypeData.GetDisputeStatusCodeType(_EbayStatConn);
                _DisputeMessageSourceCodeTypeDict = EbayCodeTypeData.GetDisputeMessageSourceCodeType(_EbayStatConn);
            }
        }

        private byte GetCurrencyId(string currencyName)
        {
            if (_CurencyCodeTypeDict.ContainsKey(currencyName))
            {
                return _CurencyCodeTypeDict[currencyName];
            }
            else
            {
                int newId;
                EbayCodeTypeData.InsertCurrencyCodeType(currencyName, _EbayStatConn, out newId);
                EbayErrorData.InsertErrorCodeType("CurrencyCodeType", newId, currencyName, "CurrencyCodeType Not Found, New CurrencyID was Generated", _EbayStatConn);
                PreLoadCodeType();
                return _CurencyCodeTypeDict[currencyName];
            }
        }
        private byte GetSiteId(string siteName)
        {
            if (_SiteCodeTypeDict.ContainsKey(siteName))
            {
                return _SiteCodeTypeDict[siteName];
            }
            else
            {
                int newId;
                EbayCodeTypeData.InsertSiteCodeType(siteName, _EbayStatConn, out newId);
                EbayErrorData.InsertErrorCodeType("SiteCodeType", newId, siteName, "SiteCodeType Not Found, New SiteID was Generated", _EbayStatConn);
                PreLoadCodeType();
                return _SiteCodeTypeDict[siteName];
            }
        }
        private byte GetListingTypeId(string listingType)
        {
            if (_ListingTypeCodeTypeDict.ContainsKey(listingType))
            {
                return _ListingTypeCodeTypeDict[listingType];
            }
            else
            {
                int newId;
                EbayCodeTypeData.InsertListingTypeCodeType(listingType, _EbayStatConn, out newId);
                EbayErrorData.InsertErrorCodeType("ListingTypeCodeType", newId, listingType, "ListingTypeCodeType Not Found, New ListingTypeID was Generated", _EbayStatConn);
                PreLoadCodeType();
                return _ListingTypeCodeTypeDict[listingType];
            }
        }
        private byte GetListingDurationId(string listingDurationName)
        {
            if (_ListingDurationCodeTypeDict.ContainsKey(listingDurationName))
            {
                return _ListingDurationCodeTypeDict[listingDurationName];
            }
            else
            {
                int newId;
                EbayCodeTypeData.InsertListingDurationCodeType(listingDurationName, _EbayStatConn, out newId);
                EbayErrorData.InsertErrorCodeType("ListingDurationCodeType", newId,listingDurationName, "ListingDurationICodeType Not Found, New ListingDurationID was Generated", _EbayStatConn);
                PreLoadCodeType();
                return _ListingDurationCodeTypeDict[listingDurationName];
            }
        }
        private byte GetShippingServiceId(string shippingService, bool expedited)
        {
            if (_ShippingServiceCodeTypeDict.ContainsKey(shippingService))
            {
                return _ShippingServiceCodeTypeDict[shippingService];
            }
            else
            {
                int newId;
                EbayCodeTypeData.InsertShippingServiceCodeType(shippingService, expedited, _EbayStatConn, out newId);
                EbayErrorData.InsertErrorCodeType("ShippingServiceCodeType", newId, shippingService, "ShippingServiceCodeType Not Found, New ShippingServiceID was Generated", _EbayStatConn);
                PreLoadCodeType();
                return _ShippingServiceCodeTypeDict[shippingService];
            }
        }


        private byte GetBidActionId(string actionName)
        {
            if (_BidActionCodeTypeDict.ContainsKey(actionName))
            {
                return _BidActionCodeTypeDict[actionName];
            }
            else
            {
                int newId;
                EbayCodeTypeData.InsertBidActionCodeType(actionName, _EbayStatConn, out newId);
                EbayErrorData.InsertErrorCodeType("BidActionCodeType", newId, actionName, "BidActionCodeType Not Found, New ActionID was Generated", _EbayStatConn);
                PreLoadCodeType();
                return _BidActionCodeTypeDict[actionName];
            }
        }
        private byte GetGalleryTypeId(string galleryTypeName)
        {
            if (_GalleryCodeTypeDict.ContainsKey(galleryTypeName))
            {
                return _GalleryCodeTypeDict[galleryTypeName];
            }
            else
            {
                int newId;
                EbayCodeTypeData.InsertGalleryTypeCodeType(galleryTypeName, _EbayStatConn, out newId);
                EbayErrorData.InsertErrorCodeType("GalleryTypeCodeType", newId, galleryTypeName, "GalleryTypeCodeType Not Found, New GalleryTypeCodeType was Generated", _EbayStatConn);
                PreLoadCodeType();
                return _GalleryCodeTypeDict[galleryTypeName];


            }
        }
        private byte GetListingEnhancementCodeTypeId(string listingEnhancementTypeName)
        {
            if (_ListingEnhancementCodeTypeDict.ContainsKey(listingEnhancementTypeName))
            {
                return _ListingEnhancementCodeTypeDict[listingEnhancementTypeName];
            }
            else
            {
                int newId;
                EbayCodeTypeData.InsertListingEnhancementCodeType(listingEnhancementTypeName, _EbayStatConn, out newId);
                EbayErrorData.InsertErrorCodeType("ListingEnhancementCodeType", newId, listingEnhancementTypeName, "ListingEnhancementCodeType Not Found, New ListingEnhancementCodeType was Generated", _EbayStatConn);
                PreLoadCodeType();
                return _ListingEnhancementCodeTypeDict[listingEnhancementTypeName];


            }
        }
        private byte GetMessageStatusTypeId(string messageStatus)
        {
            if (_MessageStatusTypeCodeTypeDict.ContainsKey(messageStatus))
            {
                return _MessageStatusTypeCodeTypeDict[messageStatus];
            }
            else
            {
                int newId;
                EbayCodeTypeData.InsertMessageStatusTypeCodeType(messageStatus, _EbayStatConn, out newId);
                EbayErrorData.InsertErrorCodeType("MessageStatusTypeCodeType", newId, messageStatus, "MessageStatusTypeCodeType Not Found, New MessageStatusID was Generated", _EbayStatConn);
                PreLoadCodeType();
                return _MessageStatusTypeCodeTypeDict[messageStatus];
            }
        }
        private byte GetMessageTypeId(string messageType)
        {
            if (_MessageTypeCodeTypeDict.ContainsKey(messageType))
            {
                return _MessageTypeCodeTypeDict[messageType];
            }
            else
            {
                int newId;
                EbayCodeTypeData.InsertMessageTypeCodeType(messageType, _EbayStatConn, out newId);
                EbayErrorData.InsertErrorCodeType("MessageTypeCodeType", newId, messageType, "MessageTypeCodeType Not Found, New MessageTypeID was Generated", _EbayStatConn);
                PreLoadCodeType();
                return _MessageTypeCodeTypeDict[messageType];
            }
        }
        private byte GetQuestionTypeId(string questionType)
        {
            if (_QuestionTypeCodeTypeDict.ContainsKey(questionType))
            {
                return _QuestionTypeCodeTypeDict[questionType];
            }
            else
            {
                int newId;
                EbayCodeTypeData.InsertQuestionTypeCodeType(questionType, _EbayStatConn, out newId);
                EbayErrorData.InsertErrorCodeType("QuestionTypeCodeType", newId, questionType, "QuestionTypeCodeType Not Found, New QuestionTypeID was Generated", _EbayStatConn);
                PreLoadCodeType();
                return _QuestionTypeCodeTypeDict[questionType];
            }
        }

        private byte GetDisputeReasonId(string disputeReason)
        {
            if (_DisputeReasonCodeTypeDict.ContainsKey(disputeReason))
            {
                return _DisputeReasonCodeTypeDict[disputeReason];
            }
            else
            {
                int newId;
                EbayCodeTypeData.InsertDisputeReasonCodeType(disputeReason, _EbayStatConn, out newId);
                EbayErrorData.InsertErrorCodeType("DisputeReasonCodeType", newId, disputeReason, "DisputeReasonCodeType Not Found, New DisputeReasonID was Generated", _EbayStatConn);
                _DisputeReasonCodeTypeDict = EbayCodeTypeData.GetDisputeReasonCodeType(_EbayStatConn);
                return _DisputeReasonCodeTypeDict[disputeReason];
            }
        }
        private byte GetDisputeExplanationId(string disputeExplanation)
        {
            if (_DisputeExplanationCodeTypeDict.ContainsKey(disputeExplanation))
            {
                return _DisputeExplanationCodeTypeDict[disputeExplanation];
            }
            else
            {
                int newId;
                EbayCodeTypeData.InsertDisputeExplanationCodeType(disputeExplanation, _EbayStatConn, out newId);
                EbayErrorData.InsertErrorCodeType("DisputeExplanationCodeType", newId, disputeExplanation, "DisputeExplanationCodeType Not Found, New DisputeExplanationID was Generated", _EbayStatConn);
                _DisputeExplanationCodeTypeDict = EbayCodeTypeData.GetDisputeExplanationCodeType(_EbayStatConn);
                return _DisputeExplanationCodeTypeDict[disputeExplanation];
            }
        }
        private byte GetDisputeCreditEligibilityId(string disputeCreditEligibility)
        {
            if (_DisputeCreditEligibilityCodeTypeDict.ContainsKey(disputeCreditEligibility))
            {
                return _DisputeCreditEligibilityCodeTypeDict[disputeCreditEligibility];
            }
            else
            {
                int newId;
                EbayCodeTypeData.InsertDisputeCreditEligibilityCodeType(disputeCreditEligibility, _EbayStatConn, out newId);
                EbayErrorData.InsertErrorCodeType("DisputeCreditEligibilityCodeType", newId, disputeCreditEligibility, "DisputeCreditEligibilityCodeType Not Found, New DisputeCreditEligibilityID was Generated", _EbayStatConn);
                _DisputeCreditEligibilityCodeTypeDict = EbayCodeTypeData.GetDisputeCreditEligibilityCodeType(_EbayStatConn);
                return _DisputeCreditEligibilityCodeTypeDict[disputeCreditEligibility];
            }
        }
        private byte GetDisputeRecordTypeId(string disputeRecordType)
        {
            if (_DisputeRecordTypeCodeTypeDict.ContainsKey(disputeRecordType))
            {
                return _DisputeRecordTypeCodeTypeDict[disputeRecordType];
            }
            else
            {
                int newId;
                EbayCodeTypeData.InsertDisputeRecordTypeCodeType(disputeRecordType, _EbayStatConn, out newId);
                EbayErrorData.InsertErrorCodeType("DisputeRecordTypeCodeType", newId, disputeRecordType, "DisputeRecordTypeCodeType Not Found, New DisputeRecordTypeID was Generated", _EbayStatConn);
                _DisputeRecordTypeCodeTypeDict = EbayCodeTypeData.GetDisputeRecordTypeCodeType(_EbayStatConn);
                return _DisputeRecordTypeCodeTypeDict[disputeRecordType];
            }
        }
        private byte GetDisputeStateId(string disputeState)
        {
            if (_DisputeStateCodeTypeDict.ContainsKey(disputeState))
            {
                return _DisputeStateCodeTypeDict[disputeState];
            }
            else
            {
                int newId;
                EbayCodeTypeData.InsertDisputeStateCodeType(disputeState, _EbayStatConn, out newId);
                EbayErrorData.InsertErrorCodeType("DisputeStateCodeType", newId, disputeState, "DisputeStateCodeType Not Found, New DisputeStateID was Generated", _EbayStatConn);
                _DisputeStateCodeTypeDict = EbayCodeTypeData.GetDisputeStateCodeType(_EbayStatConn);
                return _DisputeStateCodeTypeDict[disputeState];
            }
        }
        private byte GetDisputeStatusId(string disputeStatus)
        {
            if (_DisputeStatusCodeTypeDict.ContainsKey(disputeStatus))
            {
                return _DisputeStatusCodeTypeDict[disputeStatus];
            }
            else
            {
                int newId;
                EbayCodeTypeData.InsertDisputeStatusCodeType(disputeStatus, _EbayStatConn, out newId);
                EbayErrorData.InsertErrorCodeType("DisputeStatusCodeType", newId, disputeStatus, "DisputeStatusCodeType Not Found, New DisputeStatusID was Generated", _EbayStatConn);
                _DisputeStatusCodeTypeDict = EbayCodeTypeData.GetDisputeStatusCodeType(_EbayStatConn);
                return _DisputeStatusCodeTypeDict[disputeStatus];
            }
        }
        private byte GetDisputeMessageSourceId(string disputeMessageSource)
        {
            if (_DisputeMessageSourceCodeTypeDict.ContainsKey(disputeMessageSource))
            {
                return _DisputeMessageSourceCodeTypeDict[disputeMessageSource];
            }
            else
            {
                int newId;
                EbayCodeTypeData.InsertDisputeMessageSourceCodeType(disputeMessageSource, _EbayStatConn, out newId);
                EbayErrorData.InsertErrorCodeType("DisputeMessageSourceCodeType", newId, disputeMessageSource, "DisputeMessageSourceCodeType Not Found, New DisputeMessageSourceID was Generated", _EbayStatConn);
                _DisputeMessageSourceCodeTypeDict = EbayCodeTypeData.GetDisputeMessageSourceCodeType(_EbayStatConn);
                return _DisputeMessageSourceCodeTypeDict[disputeMessageSource];
            }
        }
    } 
}
